﻿using System;
using System.Diagnostics.Contracts;
using Beaker.Injection;
using OpenCube.Nbt.Mapping.Converters;

namespace OpenCube.Nbt.Mapping
{
	public sealed class NbtMapperFactory
		: INbtMapperFactory
	{
		private readonly IBuildInConverterFactory _converterFactory;
		private readonly IQueryContainer _queryContainer;

		public NbtMapperFactory(IBuildInConverterFactory converterFactory, IQueryContainer queryContainer)
		{
			Contract.Requires<ArgumentNullException>(converterFactory != null);
			Contract.Requires<ArgumentNullException>(queryContainer != null);
			_converterFactory = converterFactory;
			_queryContainer = queryContainer;
		}

		public INbtMapper Create<TModel>()
			where TModel : class, new()
		{
			return new NbtMapper<TModel>(_converterFactory, _queryContainer);
		}

		public INbtMapper Create(Type modelType)
		{
			Type genType = typeof(NbtMapper<>).MakeGenericType(modelType);
			return (INbtMapper)Activator.CreateInstance(genType, _converterFactory, _queryContainer);
		}
	}
}
